package com.yonyou.crm.sys.dataright.handler;

import java.util.ArrayList;
import java.util.List;

import com.yonyou.crm.sys.dataright.entity.RightObject;
import com.yonyou.crm.sys.dataright.entity.SuperVO;
import com.yonyou.crm.util.QueryUtils;

public class MultiOrgHandler implements RightHandler {

	//权限实体信息
	RightObject rightObj;

	//有权部门集合
	List<Long> validOrgs;
	
	public MultiOrgHandler(RightObject rightObj, List<Long> validOrgs) {
		super();
		this.rightObj = rightObj;
		this.validOrgs = validOrgs;
	}

	@Override
	public boolean hasRight(SuperVO vo) {
		return validOrgs.contains(getOrg(vo));
	}

	@Override
	public SuperVO[] filterWithRight(SuperVO[] vos) {
		if(vos == null || vos.length == 0) {
			return vos;
		}
		List<Object> result = new ArrayList<Object>();
		for(int i=0,len=vos.length;i<len;i++) {
			if(validOrgs.contains(getOrg(vos[i]))) {
				result.add(vos[i]);
			}
		}
		return result.toArray(new SuperVO[0]);
	}

	@Override
	public String getRightSql() {
		QueryUtils sql = new QueryUtils();
		return sql.append(rightObj.getOrgField(), validOrgs.toArray(new Long[0])).toString() ;
	}
	
	private Long getOrg(SuperVO vo) {
		return vo.getOrgId();
	}
}